/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

const { build } = require("vite");

/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function (nums) {
  function buildBST(left, right) {
    if (left > right) return null

    // 构造当前的根节点
    const mid = Math.floor((left + right) / 2)
    const node = new TreeNode(nums[mid])
    //递归构建左右子树
    node.left = buildBST(left, mid - 1)
    node.right = buildBST(mid + 1, right)

    return node
  }

  return buildBST(0, nums.length - 1)
};